package p2pEngine;
import java.util.Random;

public class Scrambler{
		public Scrambler(){}

		//Swap idx letter in string with last letter in string.
		public String[] SwapNCut( String str , int idx ){		
			char[] x = str.toCharArray();
			char temp = x[ idx ];
			x[ idx ] = x[ str.length()-1 ];
			x[ str.length()-1 ] = temp;
			String prime = new String(x);
			String x1 = prime.substring(0,prime.length()-1);
			String x2 = prime.substring(prime.length()-1,prime.length());
			String[] x3 = new String[3];
			x3[0] = x1;
			x3[1] = x2;
			return x3;
		}
		
		public String scrambleWord (String str)
		{
			StringBuffer newStr = new StringBuffer();
			
			
			while ( str.length() > 0 ){
				
				if ( str.length() == 1 ){
					newStr.append( str );
					break;
				}
				
				Random randomGenerator = new Random();
				int randomInt = randomGenerator.nextInt( str.length()-1 );
				String[] swapped = SwapNCut( str , randomInt );
				str = swapped[0];
				newStr.append( swapped[1] );
			}
			
			return newStr.toString();
		}
}

